<template>
    <div>

        <div style="margin: 10px 0">
            <el-input v-model="name" style="width: 200px" placeholder="请输入icon名称"
                      suffix-icon="el-icon-search"></el-input>
            <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
            <el-button type="warning" @click="reset">重置</el-button>
        </div>

        <div style="margin: 10px 0">
            <el-button type="primary" @click="open">新增 <i class="el-icon-circle-plus-outline"></i></el-button>
            <el-button type="danger" @click="deleteBatch">批量删除 <i class="el-icon-remove-outline"></i>
            </el-button>
        </div>

        <el-table :data="tableData" border stripe :header-cell-class-name="headerBg"
                  @selection-change="handleSelectionChange" size="medium"
        >

            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="200">
            </el-table-column>
            <el-table-column prop="name" label="名称" width="200">
            </el-table-column>
            <el-table-column prop="value" label="样式">
                <template slot-scope="scope">
                    <span :class="scope.row.value"/>
                </template>
            </el-table-column>
            <el-table-column prop="value" label="内容">
            </el-table-column>


            <el-table-column prop="type" label="类型">
            </el-table-column>

            <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                    <el-button type="primary" @click="edit(scope.row)" icon="el-icon-edit" circle></el-button>

                    <!--删除-->
                    <el-button @click="delete1(scope.row.id)"
                               slot="reference" type="danger" icon="el-icon-delete" circle
                               style="margin-left: 10px"></el-button>

                </template>
            </el-table-column>
        </el-table>
        <div style="padding: 10px 0">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog
                title="新增数据"
                :visible.sync="dialogVisible"
                width="30%"
               >
            <el-form :model="form" label-width="120px" size="medium"  :rules="rules" ref="form" class="demo-ruleForm">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="内容"  prop="value">
                    <el-input v-model="form.value" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="样式"  prop="type">
                    <el-input v-model="form.type" autocomplete="off"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm('form')">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>

        </el-dialog>


    </div>
</template>

<script>
    import request from '../utils/request'

    export default {
        name: "Dict",
        data() {
            return {
                rules: {
                    name: [
                        {required: true, message: '请输入名称', trigger: 'blur'},
                        // {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    value: [
                        {required: true, message: '请输入内容', trigger: 'blur'},
                        // {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请输入样式', trigger: 'blur'},
                        // {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                },
                roles: [],
                headerBg: 'headerBg',
                tableData: [],
                total: 0,
                pageNum: 1,
                pageSize: 5,
                name: "",

                dialogVisible: false,
                form: {},
                //批量删除
                multipleSelection: []
            }
        },
        created() {
            //请求数据
            this.load()

        },
        methods: {
            //导出
            exportExcel() {
                window.open("http://localhost:8081/person/export")
            },
            //消除校验
            resetForm(formName) {
                console.log(formName)
                this.$nextTick(()=>{
                    this.$refs.form.resetFields();
                })

                this.dialogVisible=false

            },
            deleteBatch() {
                let ids = this.multipleSelection.map(v => v.id)  //将对象数变为纯id数组
                this.$confirm('确定批量删除数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request.post("/user1/del/batch", ids).then(res => {
                        if (this.ids == null) {
                            this.$toast({
                                type: 'warning',
                                message: '请选择数据!'
                            });
                            return
                        }
                        if (res) {
                            this.$toast({
                                type: 'success',
                                message: '批量删除成功!'
                            });
                            this.load()
                        } else {
                            this.$toast({
                                type: 'error',
                                message: '批量删除失败!'
                            });
                        }

                    })

                }).catch(() => {
                    this.$toast({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            handleSelectionChange(val) {
                console.log(val)
                this.multipleSelection = val
            },
            //删除
            delete1(id) {
                this.$confirm('确定删除此icon, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    request.delete("/dict/deleteDict/" + id).then(res => {
                        if (res) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            this.load()
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }

                    })

                }).catch(() => {
                    this.$message({
                        type: 'success',
                        message: '已取消删除'
                    });
                });
            },

            //更新
            edit(row) {
                this.form = row
                this.dialogVisible = true
            },

            //新增
            save() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {  // 表单校验合法


                request.post("/dict/save", this.form).then(res => {
                    if (res) {
                        this.$message({
                            type: "success",
                            message: "保存成功"

                        })
                        this.dialogVisible = false
                        // sessionStorage.setItem("user", JSON.stringify(this.form))
                        this.load()
                    } else {
                        this.$message({
                            type: "error",
                            message: "保存失败"
                        })
                    }
                })  }
                })
            },
            //新增弹窗
            open() {
                this.dialogVisible = true
                this.form = {}
            },
            //重置
            reset() {
                this.name = "",
                this.load()
            },

            //分页
            load() {
                request.get("/dict/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.name,

                    }
                }).then(res => {
                    console.log(res)
                    this.tableData = res.data.records
                    this.total = res.data.total
                })


                request.get("/role").then(res => {
                    console.log(res)
                    this.roles = res.data
                })

                /* fetch("http://localhost:9091/user/page?pageNum="+this.pageNum+"&pageSize="+this.pageSize+"&username="+this.username+"&nickname="+this.nickname).then(res => res.json()).then(res => {
                     console.log(res)
                     this.tableData = res.data
                     this.total = res.total
                 })*/
            },
            handleSizeChange(val) {
                this.pageSize = val
                this.load()
                console.log(`每页 ${val} 条`);
            },
            handleCurrentChange(val) {
                this.pageNum = val
                console.log(`当前页: ${val}`);
                this.load()
            }
        }
    }
</script>

<style>
    .headerBg {
        background: #eee !important;
    }
</style>
